package com.xiaoyu.lanling.feature.moment.viewholder.detail

import `in`.srain.cube.views.list.ViewHolderBase
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.xiaoyu.base.app.App
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.base.view.OnClickDebounceListener
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.moment.model.detail.LikeItem
import com.xiaoyu.lanling.media.image.ImageViewUtil
import com.xiaoyu.lanling.router.Router
import com.xiaoyu.lanling.view.UserAvatarDraweeView
import com.xiaoyu.lanling.view.text.UserNameTextView

class LikeItemViewHolder : ViewHolderBase<LikeItem>() {

    private lateinit var like: ImageView
    private lateinit var avatar: UserAvatarDraweeView
    private lateinit var name: UserNameTextView
    private lateinit var time: TextView

    private val onAvatarClickListener = object : OnClickDebounceListener() {
        override fun onClickDebounce(v: View) {
            val activity = App.getInstance().topActivity ?: return
            val itemData = v.getViewTag<LikeItem>() ?: return
            val user = itemData.user ?: return

            Router.instance.gotoUserActivity(activity, user)
        }
    }

    override fun createView(layoutInflater: LayoutInflater, parent: ViewGroup): View {
        val rootView = layoutInflater.inflate(R.layout.moment_detail_like_item, parent, false)
        like = rootView.findViewById(R.id.like)
        avatar = rootView.findViewById(R.id.avatar)
        name = rootView.findViewById(R.id.name)
        time = rootView.findViewById(R.id.time)
        avatar.setOnClickListener(onAvatarClickListener)
        return rootView
    }

    override fun showData(position: Int, itemData: LikeItem) {
        like.visibility = if (position == 0) View.VISIBLE else View.GONE
        ImageViewUtil.loadImage(avatar, itemData.loadParam)
        name.text = itemData.user.name
        time.text = itemData.time
        avatar.setViewTag(itemData)
    }
}